<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/18 0018
 * Time: 10:24
 */
namespace App\Http\Controllers;

use App\Http\Requests\StoreRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class MemberController extends BaseController{
    /*
     * 会员列表
     */
    public function  index(){

        $list = DB::table('admin_member')->get();
       return view('member.list',['lists'=>$list]);
    }

    /**
     * @param int $id
     * 编辑
     */
    public function edit($id=0){
        $info = Db::table('admin_member')->where('id',$id)->first();

        return view('member.edit',['id'=>$id,'info'=>$info]);
    }
    /**
     *增加保存
     */
    public function store(StoreRequest $request){

        $map = $request->all();
        $arr['username'] = $map['user_name'];
        $arr['mobile'] = $map['tel'];
        $arr['password'] = $map['pwd'];
        $arr['agency_id'] = $map['agency_id'];

        if(isset($map['id']) && $map['id'] !='0' ){
            $arr['updatetime'] = date('Y-m-d H:i:s',time());
            $result = Db::table('admin_member')->where('id',$map['id'])->update($arr);
        }
        else{
            $data = Db::table('admin_member')->where('username',$arr['username'])->where('mobile',$arr['mobile'])->first();

            if($data){
                return ['status'=>'0','msg'=>trans('已经有该用户')];
            }
            $arr['addtime'] = date('Y-m-d H:i:s',time());
            $result = Db::table('admin_member')->insert($arr);
        }
        return ['status'=>1,'msg'=>trans('添加成功')];
    }

    /**
     * 删除用户
     */
    public function  destroy($id){
        $result = Db::table('admin_member')->where('id',$id)->delete();
        if($result){
            return['status'=>'1','msg'=>trans('删除成功')];
        }else{
            return['status'=>'0','msg'=>trans('删除失败')];
        }
    }


}